function ChangeRoot(field, url) {
  var root = field.options[field.selectedIndex].value;
  window.location = url + "&Root=" + root;
}

function ChangePerPage(field, url) {
  window.location = url + "&PerPage=" + field.value;
}

function ChangeSort(field, by, url) {
  window.location = url + "&Sort=" + field.value + "&By=" + by.value;
}

function CheckAll(field, num) {
  if (num == 1) {
    field.checked = true;
  } else {
    for (var h = 0; h < num; h++) {
      field[h].checked = true;
    }
  }
}

function ConfirmRemove(url) {
  if (url) {
    var result = window.confirm("Are you sure you want to remove this record?");
    if (result) {
      window.location = url;
    }
  } else {
    var result = window.confirm("Are you sure you want to remove the selected records?");
    if (result) {
      return true;
    } else {
      return false;
    }
  }
}

function ArrayBoxAdd(item, array) {
  if (item.value != "") {
    if (array.value == "") {
      array.value = item.value;
    } else {
      array.value = array.value + "|" + item.value;
    }
  }
  item.value = "";
}

function OpenColorPicker(cell, form, field, publicurl) {
  var picker = window.open("", "picker", "width=295,height=50");

  var c = new Array();
      c[1] = "FF";
      c[2] = "CC";
      c[3] = "99";
      c[4] = "66";
      c[5] = "33";
      c[6] = "00";

  picker.document.write("<html>\n<head>\n<title>Color Picker</title>\n</head>\n<body marginheight=\"2\" marginwidth=\"2\" topmargin=\"2\" leftmargin=\"2\">\n");

  picker.document.write("<script language=\"JavaScript\">\n");
  picker.document.write("  function returnColour(colour) {\n");
  picker.document.write("    window.opener.document.all." + cell + ".style.background = colour;\n");
  picker.document.write("    window.opener.document." + form + "." + field + ".value = colour;\n");
  picker.document.write("    window.close();\n");
  picker.document.write("  }\n");
  picker.document.write("  function previewColour(colour) {\n");
  picker.document.write("    document.all.preview.style.background = colour;\n");
  picker.document.write("  }\n");
  picker.document.write("</script>\n\n");

  picker.document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" height=\"100%\" width=\"100%\">\n");
  picker.document.write("  <tr>\n");
  picker.document.write("    <td>\n");
  picker.document.write("      <table cellpadding=\"0\" cellspacing=\"1\" border=\"0\" bgcolor=\"#000000\">\n");
  picker.document.write("        <tr>\n");

  for (h = 1; h <= 6; h++) {
    if (h > 1) {
      picker.document.write("        </tr>\n");
      picker.document.write("        <tr>\n");
    }
    for (i = 1; i <= 6; i++) {
      for (j = 1; j <= 6; j++) {
        colour = c[h] + c[i] + c[j];
        picker.document.write("          <td bgcolor=\"#" + colour + "\" width=\"7\"><a href=\"#\" onClick=\"returnColour('#" + colour + "')\" onMouseOver=\"previewColour('#" + colour + "')\"><img src=\"" + publicurl + "/Images/blank.gif\" width=\"7\" height=\"7\" border=\"0\"></td>\n");
      }
    }
  }

  picker.document.write("      </table>\n");
  picker.document.write("    </td>\n");
  picker.document.write("  </tr>\n");
  picker.document.write("  <tr>\n");
  picker.document.write("    <td><img src=\"" + publicurl + "/Images/blank.gif\" height=\"10\" border=\"0\"></td>\n");
  picker.document.write("  </tr>\n");
  picker.document.write("  <tr>\n");
  picker.document.write("    <td height=\"100%\">\n");
  picker.document.write("      <table cellpadding=\"0\" cellspacing=\"1\" border=\"0\" bgcolor=\"#000000\" width=\"100%\" height=\"100%\">\n");
  picker.document.write("        <tr>\n");
  picker.document.write("          <td id=\"preview\" bgcolor=\"#FFFFFF\" height=\"100%\"><img src=\"" + publicurl + "/Images/blank.gif\" height=\"100%\" border=\"0\"></td>\n");
  picker.document.write("        </tr>\n");
  picker.document.write("      </table>\n");
  picker.document.write("    </td>\n");
  picker.document.write("  </tr>\n");
  picker.document.write("</table>\n\n");
  picker.document.write("</body>\n</html>\n");
}

function UpdateColorPicker(cell, field) {
  cell.style.background = field.value;
}

function Tooltip(caption, message, publicurl) {
  var winTop  = (screen.height / 2) - 100;
  var winLeft = (screen.width  / 2) - 200;

  var tooltip = window.open("", "tooltip", "width=400,height=200,top=" + winTop + ",left=" + winLeft);

  tooltip.document.open();

  tooltip.document.write('<html>\n\n');
  
  tooltip.document.write('<head>\n');
  tooltip.document.write('<title>Help: ' + caption + '</title>\n');
  tooltip.document.write('<link type="text/css" href="' + publicurl + '/cpstyles.css" rel="stylesheet">\n');
  tooltip.document.write('</head>\n\n');
  
  tooltip.document.write('<body topmargin="3" leftmargin="3" rightmargin="3" bottommargin="3" marginwidth="3" marginheight="3">\n');
  tooltip.document.write('<table border="0" cellpadding="1" cellspacing="1" width="100%" height="100%" class="main-border">\n');
  tooltip.document.write('  <tr class="main-background">\n');
  tooltip.document.write('    <td>\n');
  tooltip.document.write('      <table border="0" cellpadding="1" cellspacing="0" width="100%" height="100%">\n');
  tooltip.document.write('        <tr>\n');
  tooltip.document.write('          <td height="1">\n');
  tooltip.document.write('            <table border="0" cellspacing="1" cellpadding="3" width="100%" class="row-border">\n');
  tooltip.document.write('              <tr class="header">\n');
  tooltip.document.write('                <td background="' + publicurl + '/Images/headerbg.gif">\n');
  tooltip.document.write('                  <table border="0" cellspacing="0" cellpadding="1" width="100%">\n');
  tooltip.document.write('                    <tr>\n');
  tooltip.document.write('                      <td width="2" nowrap></td>\n');
  tooltip.document.write('                      <td width="100%"><font class="header">' + caption + '</font></td>\n');
  tooltip.document.write('                    </tr>\n');
  tooltip.document.write('                  </table>\n');
  tooltip.document.write('                </td>\n');
  tooltip.document.write('              </tr>\n');
  tooltip.document.write('            </table>\n');
  tooltip.document.write('          </td>\n');
  tooltip.document.write('        </tr>\n');
  tooltip.document.write('        <tr>\n');
  tooltip.document.write('          <td height="100%">\n');
  tooltip.document.write('            <table border="0" cellspacing="1" cellpadding="3" width="100%" height="100%" class="row-border">\n');
  tooltip.document.write('              <tr class="body">\n');
  tooltip.document.write('                <td valign="top" height="100%"><font class="body">' + message + '</font></td>\n');
  tooltip.document.write('              </tr>\n');
  tooltip.document.write('              <tr class="body">\n');
  tooltip.document.write('                <td align="center" height="1"><input type="button" value="Close" onClick="window.close()"></td>\n');
  tooltip.document.write('              </tr>\n');
  tooltip.document.write('            </table>\n');
  tooltip.document.write('          </td>\n');
  tooltip.document.write('        </tr>\n');
  tooltip.document.write('        <tr>\n');
  tooltip.document.write('          <td height="1">\n');
  tooltip.document.write('            <table border="0" cellspacing="1" cellpadding="3" width="100%" class="row-border">\n');
  tooltip.document.write('              <tr class="bottombar">\n');
  tooltip.document.write('                <td><font class="bottombar"></font></td>\n');
  tooltip.document.write('              </tr>\n');
  tooltip.document.write('            </table>\n');
  tooltip.document.write('          </td>\n');
  tooltip.document.write('        </tr>\n');
  tooltip.document.write('      </table>\n');
  tooltip.document.write('    </td>\n');
  tooltip.document.write('  </tr>\n');
  tooltip.document.write('</table>\n\n');

  tooltip.document.write('</body>\n');
  tooltip.document.write('</html>\n');
  
  tooltip.document.close();
}

function OpenStyleHelp(url) {
  var winTop  = (screen.height / 2) - 300;
  var winLeft = (screen.width  / 2) - 400;

  var help = window.open("", "context", "width=800,height=600,top=" + winTop + ",left=" + winLeft);
  
  help.document.open();
  help.document.write('<html>\n\n');
  
  help.document.write('<head>\n');
  help.document.write('<title>Style Context</title>\n');
  help.document.write('</head>\n\n');
  
  help.document.write('<body marginheight="0" marginwidth="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">\n');
  help.document.write('<img border="0" src="' + url + '">\n');
  help.document.write('</body>\n\n');
  
  help.document.write('</html>\n');
  help.document.close();  
}

function DblArrayAdd(item1, item2, sel) {
  if (item1.value != "" && item2.value != "") {
    var label = "\"" + item1.value + "\" => \"" + item2.value + "\"";
    sel.options[sel.options.length] = new Option(label, item1.value + "|^|" + item2.value);
    item1.value = "";
    item2.value = "";
  }
}

function DblArrayEdit(item1, item2, sel, edit, del) {
  if (CountSel(sel) == 1) {
    var option = sel.options[sel.options.selectedIndex].value.split("|^|");
    item1.value = option[0];
    item2.value = option[1];
    
    sel.options[sel.options.selectedIndex] = null;
    del.disabled = 1;
    edit.disabled = 1;
  }
}

function DblArrayDelete(item1, item2, sel, edit, del) {
  if (CountSel(sel) == 1) {
    sel.options[sel.options.selectedIndex] = null;
    del.disabled = 1;
    edit.disabled = 1;
  }
}

function DblArrayChange(sel, edit, del) {
  if (CountSel(sel) == 1) {
    del.disabled = 0;
    edit.disabled = 0;
  } else {
    del.disabled = 1;
    edit.disabled = 1;
  }
}

function DblArraySubmit(sel) {
  for (var h = 0; h < sel.options.length; h++) {
    var current = sel.options[h];
    current.selected = 1;
  }
  
  return true;
}

function CountSel(sel) {
  var count = 0;
  for (var h = 0; h < sel.options.length; h++) {
    var current = sel.options[h];
    if (current.selected) {
      count++;
    }
  }

  return count;
}